package com.bluesky.plum.uimodels.render.html.components.containers.window;

import javax.swing.JComponent;
import javax.swing.JDialog;

import com.bluesky.plum.uimodels.standard.StandardUIComponentType;
import com.bluesky.plum.uimodels.standard.components.containers.Panel;
import com.bluesky.plum.uimodels.standard.components.containers.window.Dialog;
import com.bluesky.plum.uimodels.standard.components.containers.window.Window;

/**
 * in the first edition, I intend to design the dialog in a flat way. that means
 * only the most font window will be displayed.
 * 
 * @author HZ00260
 * 
 */
public class HDialog extends Dialog {

	protected HWindowThread eventHandlerThread;
	
	public HDialog() {
		super();
		
		eventHandlerThread = new HWindowThread(this);
		eventHandlerThread.start();
	}

	@Override
	protected void init() {

		super.init();

	}

	@Override
	public void setContentPanel(Panel contentPanel) {

		super.setContentPanel(contentPanel);


	}

	@Override
	public void setModal(boolean modal) {

		super.setModal(modal);

		// dialog.setModal(modal);

	}

	/**
	 * only code from parent window thread will setVisible(true), equals
	 * showModal();<br>
	 * only code from itself's thread will setVisible(false), equals endModal();
	 */
	@Override
	public void setVisible(boolean visible) {

		super.setVisible(visible);

		if (isModal()) {

			/**
			 * to show a modal dialog, suspend the parent window's thread. after
			 * some time, the this dialog's thread will notify it.
			 */

			if (visible) {
				System.out
						.println("setVisible(), it is a modal dialog, so the thread is paused.");

				// pause current thread
				HWindowThread wt = (HWindowThread) Thread.currentThread();

				// resume servlet thread
				Event event = wt.getCurrentEvent();
				synchronized (event) {
					System.out
					.println("setVisible(), servlet thread will be resumed.");

					event.notify();// let the servletThread proceed
				}

				wt.pause();

				System.out.println("setVisible(), the thread resumed.");

				/*
				 * 
				 * the second half, the thread of window is awake
				 */

			} else {
				/**
				 * to hide a modal dialog, resume parent window's thread.
				 * 
				 * only the dialog's own thread will hide itself.
				 */
				HWindowThread wtParent = (HWindowThread) getParentWindow()
						.getEventHandlerThread();
				HWindowThread wtItself = (HWindowThread) getEventHandlerThread();

				// set the current event for the parent window's thread, so it
				// can notify it later
				wtParent.setCurrentEvent(wtItself.getCurrentEvent());
				// let the parent window thread alive.
				wtParent.proceed();

				// kill itself

				wtItself.putCloseEvent();

			}
		}

		// dialog.setVisible(visible);
	}

	@Override
	public Object getNativeComponent() {

		return contentPanel.getNativeComponent();
	}

	@Override
	public void setParentWindow(Window parentWindow) {

		super.setParentWindow(parentWindow);

		contentPanel = (Panel) getUiComponentBuilder().createNonFieldComponent(
				StandardUIComponentType.Panel);
	}

	@Override
	public void pack() {

		// wholeBox.validate();
		// dialog.pack();
	}

	@Override
	public void setTitle(String title) {

		super.setTitle(title);

		//for debug sake
		eventHandlerThread.refreshName();
		
		
		// dialog.setTitle(title);
	}

	public void closeModalDialog() {

	}

	@Override
	public Thread getEventHandlerThread() {
		return eventHandlerThread;
	}

	public void setEventHandlerThread(HWindowThread eventHandlerThread) {
		this.eventHandlerThread = eventHandlerThread;
	}
}
